package com.tutu.cart.listener;

import com.tutu.cart.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Program: b2c-cloud-store
 * @Description: 监听rabbitmq消息
 * @Author: JK.TuTu
 * @Version: 1.0
 * @Create: 2022-12-28 15:14
 **/
@Slf4j
@Component
public class RabbitMqListener {


    @Autowired
    private CartService cartService;

    /**
     * 监听传给购物车服务的rabbitmq消息
     *
     * @param cartIds 购物车id集合
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "clear.queue"),
            exchange = @Exchange(value = "topic.ex", type = ExchangeTypes.TOPIC),
            key = "clear.cart"))
    public void clear(List<Integer> cartIds) {
        log.info("接收到的消息是:{}", cartIds);
        //调用业务层方法删除购物车中的数据
        cartService.removeCarts(cartIds);
    }
}
